package com.ruoyi.purchase.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.base.domain.Customer;
import com.ruoyi.base.domain.GasBalance;
import com.ruoyi.base.mapper.CustomerMapper;
import com.ruoyi.base.mapper.GasBalanceMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.purchase.mapper.PurchaseRecordMapper;
import com.ruoyi.purchase.domain.PurchaseRecord;
import com.ruoyi.purchase.service.IPurchaseRecordService;

/**
 * 采购记录信息
Service业务层处理
 *
 * @author h w
 * @date 2022-12-09
 */
@Service
public class PurchaseRecordServiceImpl implements IPurchaseRecordService
{
    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private GasBalanceMapper gasBalanceMapper;

    /**
     * 查询采购记录信息

     *
     * @param purchaseRecordId 采购记录信息
主键
     * @return 采购记录信息

     */
    @Override
    public PurchaseRecord selectPurchaseRecordByPurchaseRecordId(Long purchaseRecordId)
    {
        return purchaseRecordMapper.selectPurchaseRecordByPurchaseRecordId(purchaseRecordId);
    }

    /**
     * 查询采购记录信息
列表
     *
     * @param purchaseRecord 采购记录信息

     * @return 采购记录信息

     */
    @Override
    public List<PurchaseRecord> selectPurchaseRecordList(PurchaseRecord purchaseRecord)
    {
        return purchaseRecordMapper.selectPurchaseRecordList(purchaseRecord);
    }

    /**
     * 新增采购记录信息

     *
     * @param purchaseRecord 采购记录信息

     * @return 结果
     */
    @Override
    public int insertPurchaseRecord(PurchaseRecord purchaseRecord)
    {
        GasBalance balance = new GasBalance();
        balance.setCustomerId(purchaseRecord.getPurchaseUnitId());
        balance.setEnterpriseId(purchaseRecord.getSaleUnitId());
        List<GasBalance> balanceList = gasBalanceMapper.selectGasBalanceList(balance);
        if(balanceList != null && balanceList.size() > 0) {
            GasBalance balanceModify = balanceList.get(0);
            BigDecimal rest = balanceModify.getCustomerBalance().subtract(purchaseRecord.getTotalAmount());
            if(rest.compareTo(BigDecimal.ZERO) > 0) {
                balanceModify.setCustomerBalance(rest);
                balanceModify.setUpdateTime(DateUtils.getNowDate());
                balanceModify.setUpdateBy(purchaseRecord.getCreateBy());
                gasBalanceMapper.updateGasBalance(balanceModify);
                purchaseRecord.setCreateTime(DateUtils.getNowDate());
                return purchaseRecordMapper.insertPurchaseRecord(purchaseRecord);
            } else {
                return 0;
            }
        }
        return 0;
    }

    /**
     * 修改采购记录信息

     *
     * @param purchaseRecord 采购记录信息

     * @return 结果
     */
    @Override
    public int updatePurchaseRecord(PurchaseRecord purchaseRecord)
    {
        purchaseRecord.setUpdateTime(DateUtils.getNowDate());
        return purchaseRecordMapper.updatePurchaseRecord(purchaseRecord);
    }

    /**
     * 批量删除采购记录信息

     *
     * @param purchaseRecordIds 需要删除的采购记录信息
主键
     * @return 结果
     */
    @Override
    public int deletePurchaseRecordByPurchaseRecordIds(Long[] purchaseRecordIds)
    {
        return purchaseRecordMapper.deletePurchaseRecordByPurchaseRecordIds(purchaseRecordIds);
    }

    /**
     * 删除采购记录信息
信息
     *
     * @param purchaseRecordId 采购记录信息
主键
     * @return 结果
     */
    @Override
    public int deletePurchaseRecordByPurchaseRecordId(Long purchaseRecordId)
    {
        return purchaseRecordMapper.deletePurchaseRecordByPurchaseRecordId(purchaseRecordId);
    }
}
